package org.eclipse.football;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Kicker {
	// Define private member variables
	private String name;
	private String team;
	private int pat;
	private int fg;
	private int misses;
	
	// Define constructor
	public Kicker(String name, String team){
		// Set name and team based off given input name
		this.name = name;
		this.team = team;
		
		// Initialize all other member variables to 0
		pat = 0;
		fg = 0;
		misses = 0;
	}

	// Getters and Setters
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTeam() {
		return team;
	}
	
	public void setTeam(String team) {
		this.team = team;
	}
	
	public int getPat() {
		return pat;
	}

	public void setPat(int pat) {
		this.pat = pat;
	}

	public int getFg() {
		return fg;
	}

	public void setFg(int fg) {
		this.fg = fg;
	}

	public int getMisses() {
		return misses;
	}

	public void setMisses(int misses) {
		this.misses = misses;
	}

	// Define a function to calculate this kickers score in fantasy
	public int calculatePoints(){
		// Initialize sum to 0
		int sum = 0;
		
		//TODO: JUST A MARKER FOR THIS MALARKY
		updatePerformanceFromFile(17);
		
		// Add points for pat
		sum += pat;
		
		// Add points for each category of field goal made
		sum += 3*fg;
		
		// Subtract points for misses
		sum -= misses;
		
		// Return sum
		return sum;
	}

	// Define function to check the specified CSV for this player's
	// actual game performance
	public boolean updatePerformanceFromFile(Integer week){
		ArrayList<File> ourFiles = new ArrayList<File>();
		File kicking;
		FileReader inputFileReader = null;
		for (int i=1;i<week;i++){
			kicking = new File("kicking" + i + "clean.txt");
			if ( kicking.exists()) {
				ourFiles.add(kicking);
			} else {
				System.err.println("kicking" + i + "clean.txt not found");
			}
		}
		pat = 0;
		fg = 0;
		misses = 0;
		int temp = 0;
		String theCurrentLine;
		ArrayList<String> theCurrentLineSplit;
		BufferedReader fr = null;
		Scanner sc = null;
		boolean found = false;
		String pos = null;
		for (File fileToCheck:ourFiles){
			try 
			{
				//Attach to file
				fr = new BufferedReader(new FileReader(fileToCheck));	
				//Read from file only if ready to be read
				while (fr.ready()){
					//Read line as String
					theCurrentLine= fr.readLine();
					//Split Line into parts to find the name at the front
					theCurrentLineSplit = new ArrayList<String>(Arrays.asList(theCurrentLine.split(",")));
					//Check the name at the start of the line to match this player
					if (theCurrentLineSplit.get(1).trim().contains(name.trim())){
						//Scanner == String Parser
						sc = new Scanner(theCurrentLine).useDelimiter(",");
						//EXAMPLE LINE:
						//fgmade fgatt xpmade xpatt pts
						//1,Mason Crosby, K,GB,4,4,3,3,15,15
						//Skip the rank at the front
						if (sc.hasNextInt()) { sc.nextInt(); }
						//Skip the name
						if (sc.hasNext())    { sc.next(); }
						//Skip the position
						if (sc.hasNext())    { pos = sc.next(); }
						//Skip the team
						if (sc.hasNext())    { sc.next(); }
						//fgmade
						if (sc.hasNextInt()) { temp = sc.nextInt(); }
						fg += temp;
						//fgatt
						if (sc.hasNextInt()) { misses += (sc.nextInt() - temp); }
						//xpmade
						if (sc.hasNextInt()) { pat += sc.nextInt(); }
						//xpatt
//						if (sc.hasNextInt()) { pass_tds += sc.nextInt(); }
						//System.out.println(fg1 + "," + misses + "," + pat);
						found = true;
						break;
					}
				}
			}
			catch (IOException ioe)
			{
				ioe.printStackTrace();
			}
			finally
			{
				if (fr != null && sc != null)
				{
					try
					{
						fr.close();
						sc.close();
					}
					catch (IOException ioe)
					{
						ioe.printStackTrace();
					}
				}
			}
		}
		if (!found){
			return false;
		}
		return true;
	}
}